package com.jiamao.goplay.adapter;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.LayoutRes;
import androidx.annotation.RequiresApi;
import androidx.databinding.ViewDataBinding;
import androidx.viewpager.widget.ViewPager;

import com.jiamao.goplay.R;
import com.jiamao.goplay.ui.discover.vm.CityMenuItemVM;
import com.jiamao.goplay.ui.discover.vm.FindItemVM;
import com.rd.PageIndicatorView;

import me.tatarka.bindingcollectionadapter2.BindingRecyclerViewAdapter;

public class FindAdapter extends BindingRecyclerViewAdapter {

    Context context;
    public FindAdapter(Context context){
        this.context = context;
    }

    @Override
    public ViewDataBinding onCreateBinding(LayoutInflater inflater, @LayoutRes int layoutId, ViewGroup viewGroup) {
        ViewDataBinding binding = super.onCreateBinding(inflater, layoutId, viewGroup);
        return binding;
    }

    private int iondex = 0 ;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindBinding(ViewDataBinding binding, int bindingVariable, @LayoutRes int layoutId, int position, Object item) {
        super.onBindBinding(binding, bindingVariable, layoutId, position, item);
                if (item instanceof FindItemVM){

                    FindItemVM vm  = (FindItemVM) item;
                    if(vm.item!=null) {

                        PageIndicatorView pageIndicatorView = binding.getRoot().findViewById(R.id.pageIndicatorView);
                        pageIndicatorView.setCount(vm.item.getAvatarList().size()); // specify total count of indicators
                        pageIndicatorView.setSelection(0);
                        ViewPager viewPager = binding.getRoot().findViewById(R.id.view_pager);
                        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                            @Override
                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {/*empty*/}

                            @Override
                            public void onPageSelected(int position) {
                                pageIndicatorView.setSelection(position);
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {/*empty*/}
                        });
                        binding.getRoot().findViewById(R.id.iv_next).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                vm.index.set(position);
                                vm.itemNextClick.execute();
                            }
                        });
                        ImageView imageView =binding.getRoot().findViewById(R.id.img_sz);

                        switch (vm.sz.get()){
                            case "白羊座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_byz);
                                break;
                            case "金牛座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_jnz);
                                break;
                            case "双子座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_shuangziz);
                                break;
                            case "巨蟹座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_jxz);
                                break;
                            case "狮子座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_szz);
                                break;
                            case "处女座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_cnz);
                                break;
                            case "天秤座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_tpz);
                                break;
                            case "天蝎座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_txz);
                                break;
                            case "射手座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_ssz);
                                break;
                            case "摩羯座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_mzx);
                                break;
                            case "水平座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_spz);
                                break;
                            case "双鱼座":
                                imageView.setImageResource(R.mipmap.icon_user_xz_syz);
                                break;

                        }
                    }

             }
    }

    public void refreshUI(){

        CityMenuItemVM vm = (CityMenuItemVM) getAdapterItem(iondex);
        vm.select.set(false);
//        notifyItemChanged(iondex);
        notifyDataSetChanged();

    }

//    @Override
//    public void onBindBinding(@NonNull ViewDataBinding binding, int variableId, int layoutRes, int position, Object item) {
//        super.onBindBinding(binding, variableId, layoutRes, position, item);
//
//        if (item instanceof CityMenuItemVM){
//            CityMenuItemVM vm  = (CityMenuItemVM) item;
//
//        }
//
//    }
}
